package com.sykj.bsc.rwl.service.Impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sykj.bsc.rwl.mapper.SysMenuMapper;
import com.sykj.bsc.rwl.mapper.SysUserMapper;
import com.sykj.bsc.rwl.po.SysMenu;
import com.sykj.bsc.rwl.po.SysRole;
import com.sykj.bsc.rwl.po.Ztree;
import com.sykj.bsc.rwl.po.ZtreeBean;
import com.sykj.bsc.rwl.po.extend.SysMenuExtend;
import com.sykj.bsc.rwl.service.SysMenuService;
import com.sykj.bsc.rwl.util.JedisClientPool;
import com.sykj.bsc.rwl.util.JsonUtils;

@Service("sysMenuService")
public class SysMenuServiceImpl implements SysMenuService{
	
	@Autowired
	private SysMenuMapper sysMenuMapper;
	@Autowired
	private SysUserMapper sysUserMapper;
	private ZtreeBean ztreeBean;
	private Ztree ztree;
	/*@Autowired
	private JedisClientPool pool;*/
	
	public String selectAllMenu(int user_id) throws Exception {
		//String json=pool.get("findByIdSysRole"+user_id+"sysRolew1");
		SysRole sysRole=sysUserMapper.findByIdSysRole(user_id);
		/*if(json==null) {
			sysRole=sysUserMapper.findByIdSysRole(user_id);
			json=JsonUtils.objectToJson(sysRole);
			pool.set("findByIdSysRole"+user_id+"sysRolew1", json);
		}else {
			sysRole=JsonUtils.jsonToPojo(json, SysRole.class);
		}*/
		HashMap<String, String> map=new HashMap<>();
		map.put("role_id", sysRole.getRole_id()+"");
		//String json2=pool.get("selectMenuMaplistw2");
		List<SysMenu> list=sysMenuMapper.selectMenu(map);
		/*if(json2==null) {
			list=sysMenuMapper.selectMenu(map);
			json2=JsonUtils.objectToJson(list);
			pool.set("selectMenuMaplistw2", json2);
		}else {
			list=JsonUtils.jsonToList(json2, SysMenu.class);
		}*/
		List<ZtreeBean> list2=new ArrayList<ZtreeBean>();
		for(SysMenu sys:list) {
			ztreeBean=new ZtreeBean();
			ztreeBean.setAccessPath("/bsc"+sys.getUrl());
			ztreeBean.setResourceName(sys.getMenu_name());
			ztreeBean.setParentID(Integer.valueOf(sys.getParent_id()));
			ztreeBean.setResourceID(sys.getMenu_id());
			list2.add(ztreeBean);
		}
		String data=JsonUtils.objectToJson(list2);
		return data;
	}

	@Override
	public String selectPermisson(int user_id) throws Exception {
		//String json=pool.get("findByIdSysRole"+user_id+"sysRole1w3");
		SysRole sysRole=sysUserMapper.findByIdSysRole(user_id);
		/*if(json==null) {
			sysRole=sysUserMapper.findByIdSysRole(user_id);
			json=JsonUtils.objectToJson(sysRole);
			pool.set("findByIdSysRole"+user_id+"sysRole1w3", json);
		}else {
			sysRole=JsonUtils.jsonToPojo(json, SysRole.class);
		}*/
		HashMap<String, String> map=new HashMap<>();
		List<SysMenu> list=sysMenuMapper.selectAllMenu();
		/*String json2=pool.get("selectAllMenulistw4");
		if(json2==null) {
			list=sysMenuMapper.selectAllMenu();
			json2=JsonUtils.objectToJson(list);
			pool.set("selectMenuMaplistw4", json2);
		}else {
			list=JsonUtils.jsonToList(json2, SysMenu.class);
		}*/
		List<Ztree> list2=new ArrayList<Ztree>();
		for(SysMenu sysMenu:list) {
			map.put("role_id", sysRole.getRole_id()+"");
			map.put("menu_id", sysMenu.getMenu_id()+"");
			boolean bool=getCount(map);
			ztree=new Ztree();
			ztree.setId(sysMenu.getMenu_id());
			ztree.setpId(Integer.valueOf(sysMenu.getParent_id()));
			ztree.setName(sysMenu.getMenu_name());
			ztree.setChecked(bool);
			ztree.setOpen(bool);
			list2.add(ztree);
		}
		String data=JsonUtils.objectToJson(list2);
		return data;
	}

	@Override
	public String selectPermission2(int role_id) throws Exception {
		HashMap<String, String> map=new HashMap<>();
		List<SysMenu> list=sysMenuMapper.selectAllMenu();
		/*String json=pool.get("selectAllMenulist2w5");
		if(json==null) {
			list=sysMenuMapper.selectAllMenu();
			json=JsonUtils.objectToJson(list);
			pool.set("selectAllMenulist2w5", json);
		}else {
			list=JsonUtils.jsonToList(json, SysMenu.class);
		}*/
		List<Ztree> list2=new ArrayList<Ztree>();
		for(SysMenu sysMenu:list) {
			map.put("role_id", role_id+"");
			map.put("menu_id", sysMenu.getMenu_id()+"");
			boolean bool=getCount(map);
			ztree=new Ztree();
			ztree.setId(sysMenu.getMenu_id());
			ztree.setpId(Integer.valueOf(sysMenu.getParent_id()));
			ztree.setName(sysMenu.getMenu_name());
			ztree.setChecked(bool);
			ztree.setOpen(bool);
			list2.add(ztree);
		}
		String data=JsonUtils.objectToJson(list2);
		return data;
	}

	
	public void savePermission(SysMenuExtend sysMenuExtend) throws Exception {
		sysMenuMapper.deleteAllPermission(sysMenuExtend.getRole_id());
		String data=sysMenuExtend.getData();
		String[] array=data.split(",");
		List<Integer> menu_ids=new ArrayList<>();
		for(int i=0,len=array.length;i<len;i++) {
			menu_ids.add(Integer.valueOf(array[i]));
		}
		sysMenuExtend.setMenu_ids(menu_ids);
		sysMenuMapper.insertAllPermission(sysMenuExtend);
	}

	@Override
	public boolean getCount(HashMap<String, String> map) throws Exception {
		int count=sysMenuMapper.getCount(map);
		if(count>0)return true;
		return false;
	}
}
